﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using VocabUtils;

namespace VocabGUI
{
	/// <summary>
	/// Interaction logic for WorstWordsWindow.xaml
	/// </summary>
	public partial class WordsWindow : Window
	{
		private WordsWindowViewModel viewModel;
		private DelayedJobRunner filterRunner;

		public WordsWindow( WordsWindowViewModel viewModel )
		{
			this.viewModel = viewModel;
			this.filterRunner = new DelayedJobRunner();
			InitializeComponent();

			this.DataContext = this.viewModel;
		}

		private void FilterTextBox_OnTextChanged(object sender, TextChangedEventArgs e)
		{
			string filter = this.filterTextBox.Text;
			filterRunner.RunJob( () =>
			{
				WordsWindowViewModel.ViewWord[] tmpWords = this.viewModel.Words.ToArray();

				if( filter.Length != 0 )
				{
					tmpWords = tmpWords.Where( w => w.Eng.Contains( filter ) || w.Hun.Contains( filter ) ).ToArray();
				}

				this.Dispatcher.Invoke( () =>
				{
					this.viewModel.FilteredWords.Clear();
					foreach( var viewWord in tmpWords )
					{
						this.viewModel.FilteredWords.Add( viewWord );
					}
				} );

			} );
		}
	}
}
